package sg.edu.nus.iss.umpire.service.umpire;


import org.datanucleus.util.StringUtils;

import sg.edu.nus.iss.umpire.domain.Umpire;
import sg.edu.nus.iss.umpire.support.HttpMethod;
import sg.edu.nus.iss.umpire.support.Service;
import sg.edu.nus.iss.umpire.support.ServiceConfig;
import sg.edu.nus.iss.umpire.support.ServiceContext;

@ServiceConfig(path = "umpires/edit", method = HttpMethod.POST)
public class SaveUmpireService implements Service {
	@Override
	public String serve(ServiceContext ctx) throws Exception {
		Umpire umpire = null;

		String id = ctx.getRequest().getParameter("key");
		if (StringUtils.isEmpty(id)) {
			umpire = new Umpire();
		} else {
			Long key = Long.valueOf(id);
			umpire = ctx.getEntityManager().find(Umpire.class,
					Long.valueOf(key));
		}

		String nric = ctx.getRequest().getParameter("nric");
		String name = ctx.getRequest().getParameter("name");
		String email = ctx.getRequest().getParameter("email");
		String mobile = ctx.getRequest().getParameter("mobile");
		String umpireType = ctx.getRequest().getParameter("umpireType");
		String address = ctx.getRequest().getParameter("address");

		umpire.setNric(nric);
		umpire.setName(name);
		umpire.setEmail(email);
		umpire.setMobile(mobile);
		umpire.setUmpireType(umpireType);
		umpire.setAddress(address);

		ctx.getEntityManager().merge(umpire);
		return "redirect:/s/umpires";
	}
}
